package com.shanghaionstar.remote.manager;

import com.onstar.cn.auth.userProfile.FeatureEnum;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.utils.RxBus;
import com.onstar.cn.mag.enumtype.RemoteCommandsType;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.rxconstants.RxConstants;
import com.shanghaionstar.R;
import com.shanghaionstar.nav.model.PoiHistoryInfo;
import com.shanghaionstar.remote.model.RemoteModel;
import com.shanghaionstar.sqlite.SearchHistoryImpl;
import com.shanghaionstar.sqlite.TBTHistoryImpl;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;

import java.lang.ref.SoftReference;

import static com.onstar.cn.mag.enumtype.RemoteCommandsType.COMMANDS_TBT_ROUTE;

/**
 * Created by onstarios on 2017/7/17.
 */

public class RemoteOperateValidateHelper {

    public SoftReference<BaseActivity> ctxSr;

    public RemoteOperateValidateHelper(BaseActivity baseActivity) {
        ctxSr = new SoftReference<BaseActivity>(baseActivity);
    }

    public boolean validateTbtOrOdd(PoiItemInfo poiItemInfo, RemoteCommandsType remoteCommandsType) {
        BaseActivity baseActivity = null;
        if (ctxSr != null)
            baseActivity = ctxSr.get();
        if (poiItemInfo == null) return false;
        if (remoteCommandsType != COMMANDS_TBT_ROUTE && remoteCommandsType != RemoteCommandsType.COMMANDS_NAV_DESTINATION)
            return false;
        if (!ApplicationUtil.getInstance().isLogin())
            return false;
        if (ApplicationUtil.getInstance().isAutoLogings()) {
            AlertDialogUtil.showAlertDialog(baseActivity, baseActivity.getString(R.string.loading));
        }
        if (!BusinessUtil.checkDriverOrProxyAndShowDialog(baseActivity)) {
            return false;
        }
        if (!BusinessUtil.checkPackageRight(Constants.ENTITLEMENT_M05NAVIGATION, baseActivity, true)) {
            return false;
        }
        return true;
    }

    public RemoteModel getRemoteModel() {
        BaseActivity baseActivity = null;
        if (ctxSr != null)
            baseActivity = ctxSr.get();
        RemoteModel navInfoModel = new RemoteModel.Builder().create();
        if (BusinessUtil.isServiceFeature(FeatureEnum.SENDNAVDESTINATION.value(), baseActivity)) {
            if (PreferencesUtls.getInstance(baseActivity).getBoolean(FinalConstants.SharedPreferencesConfig.TBT_CHOOSE_SAVE, true)) {
                navInfoModel.setRemoteCommandsType(RemoteCommandsType.COMMANDS_TBT_ROUTE);
            } else if (PreferencesUtls.getInstance(baseActivity).getBoolean(FinalConstants.SharedPreferencesConfig.ODD_CHOOSE_SAVE, false)) {
                navInfoModel.setRemoteCommandsType(RemoteCommandsType.COMMANDS_NAV_DESTINATION);
            }
        } else {
            navInfoModel.setRemoteCommandsType(RemoteCommandsType.COMMANDS_TBT_ROUTE);
        }
        return navInfoModel;
    }

    public int getAlertDialogByType(String commandType) {
        int alertMsg;
        RemoteCommandsType remoteCommandsType = RemoteCommandsType.fromValue(commandType);
        if (remoteCommandsType != null) {
            switch (remoteCommandsType) {
                case COMMANDS_LOCK_DOOR:
                    alertMsg = R.string.remote_lock_dialog;
                    break;
                case COMMANDS_UNLOCK_DOOR:
                    alertMsg = R.string.remote_unlock_dialog;
                    break;
                case COMMANDS_START:
                    alertMsg = R.string.remote_start_dialog;
                    break;
                case COMMANDS_CANCEL_START:
                    alertMsg = R.string.remote_stop_dialog;
                    break;
                case COMMANDS_ALERT:
                    alertMsg = R.string.remote_alert_dialog;
                    break;
                default:
                    alertMsg = R.string.is_execute_control;
                    break;
            }
        } else {
            alertMsg = R.string.is_execute_control;
        }
        return alertMsg;
    }

    public boolean validateCommandType(IMVPView remoteView) {
        return BusinessUtil.checkRemoteControl(ctxSr.get(), Constants.ENTITLEMENT_M07REMOTE_CONTROL, 0, remoteView);
    }


    public boolean isRemoteCommand(String sRemoteCommandsType) {
        RemoteCommandsType remoteCommandsType = RemoteCommandsType.fromValue(sRemoteCommandsType);
        if (remoteCommandsType == null) return false;
        return (remoteCommandsType == RemoteCommandsType.COMMANDS_LOCK_DOOR
                || remoteCommandsType == RemoteCommandsType.COMMANDS_UNLOCK_DOOR
                || remoteCommandsType == RemoteCommandsType.COMMANDS_ALERT
                || remoteCommandsType == RemoteCommandsType.COMMANDS_CANCEL_START
                || remoteCommandsType == RemoteCommandsType.COMMANDS_START);
    }

    /**
     * @param
     * @return void
     * @Title: saveSearchHistory
     * @Description: 保存到历史记录中
     */
    public void saveSearchHistory(PoiItemInfo poiItemInfo) {
        BaseActivity baseActivity = null;
        if (ctxSr != null)
            baseActivity = ctxSr.get();
        SearchHistoryImpl impl = new SearchHistoryImpl(baseActivity);
        impl.saveSearchHistory(poiItemInfo);
        RxBus.getDefault().post(RxConstants.ALL_PAGE_REFRESH, RxConstants.NAV_SAVE_POI);
    }

    public void saveHistoryToSql(String requestId, PoiItemInfo poiItemInfo, String status, boolean isSave) {
        PoiHistoryInfo historyInfo = new PoiHistoryInfo();
        historyInfo.setPoiItemInfo(poiItemInfo);
        if (isSave) {
            historyInfo.setStatus("inProgress");
        } else {
            historyInfo.setStatus(status);
        }
        BaseActivity baseActivity = null;
        if (ctxSr != null)
            baseActivity = ctxSr.get();
        TBTHistoryImpl impl = new TBTHistoryImpl(baseActivity);
        impl.saveOrUpdateModelDestinationHistory(requestId, historyInfo, isSave);
    }

    public void sendTbtOrOdd(PoiItemInfo poiItemInfo) {
        RemoteModel model = getRemoteModel();
        model.poiItemInfo = poiItemInfo;
        if(validateTbtOrOdd(poiItemInfo,model.getRemoteCommandsType())){
            RemoteOperateManager.getInstance().init(ctxSr.get()).exeRemote(model.getRemoteCommandsType(),poiItemInfo);
        }
    }

}
